home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cscop122.zip / DCALC.C < prev    next >
Text File  |  1996-05-01  |  1KB  |  42 lines

  1. #include <stdio.h>
  2.  
  3. #define MAXOP 20
  4. #define NUMBER '0'
  5. #define TOOBIG '9'
  6.  
  7. main()
  8. {
  9.   int type;
  10.   char s[MAXOP];
  11.   double op2, atof(char *), pop(void), push(double);
  12.   void clear(void);
  13.   int getop(char *s, int lim);
  14.   
  15.   while ((type = getop(s, MAXOP)) != EOF)
  16.     switch (type)
  17.     { case NUMBER: push(atof(s));
  18.                    break;
  19.       case '+':    push(pop() + pop());
  20.                    break;
  21.       case '*':    push(pop() * pop());
  22.                    break;
  23.       case '-':    op2 = pop();
  24.                    push(pop() - op2);
  25.                    break;
  26.       case '/':    op2 = pop();
  27.                    if (op2 != 0.0)
  28.                      push(pop() / op2);
  29.                    else
  30.                      printf("zero divisor popped\n");
  31.                    break;
  32.       case '=':    printf("\t%f\n",push(pop()));
  33.                    break;
  34.       case 'c':    clear();
  35.                    break;
  36.       case TOOBIG: printf("%.20s ... is too long\n",s);
  37.                    break;
  38.       default:     printf("unknown command %c\n", type);
  39.                    break;
  40.     }
  41. }
  42.